package Tools.Crop
{
	import flash.display.Graphics;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.UIComponent;

	public class CropRectComponent extends UIComponent
	{
		public function CropRectComponent()
		{
			super();
		}
		
		private var _topLeftPoint:Point;

		public function get topLeftPoint():Point
		{
			return _topLeftPoint;
		}

		public function set topLeftPoint(value:Point):void
		{
			_topLeftPoint = value;
			invalidateDisplayList();
		}

		
		private var _bottomRightPoint:Point;

		public function get bottomRightPoint():Point
		{
			return _bottomRightPoint;
		}

		public function set bottomRightPoint(value:Point):void
		{
			_bottomRightPoint = value;
			invalidateDisplayList();
		}

		
		private var _parentRec:Rectangle;

		public function get parentRec():Rectangle
		{
			return _parentRec;
		}

		public function set parentRec(value:Rectangle):void
		{
			_parentRec = value;
			invalidateDisplayList();
		}
		
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			if( topLeftPoint && bottomRightPoint && parentRec )
			{
				draw();
			}
		}
		
		private function draw():void
		{
			var g:Graphics = this.graphics;
			g.clear();
			g.beginFill(0xcccccc,.6);
			g.drawRect(0,0,parentRec.width,topLeftPoint.y);
			g.drawRect(0,bottomRightPoint.y,parentRec.width,parentRec.height-bottomRightPoint.y);
			g.drawRect(0,topLeftPoint.y,topLeftPoint.x,bottomRightPoint.y-topLeftPoint.y);
			g.drawRect(bottomRightPoint.x,topLeftPoint.y,parentRec.width-bottomRightPoint.x,bottomRightPoint.y-topLeftPoint.y);
			g.endFill();
			g.lineStyle(1,0xffffff);
			g.beginFill(0xffffff,.1);
			g.drawRect(topLeftPoint.x,topLeftPoint.y,bottomRightPoint.x-topLeftPoint.x,bottomRightPoint.y-topLeftPoint.y);
			g.endFill();
			g.moveTo(topLeftPoint.x,topLeftPoint.y + (bottomRightPoint.y - topLeftPoint.y)/2 );
			g.lineTo(bottomRightPoint.x, topLeftPoint.y + (bottomRightPoint.y - topLeftPoint.y)/2);
			g.moveTo(topLeftPoint.x + (bottomRightPoint.x-topLeftPoint.x)/2,topLeftPoint.y );
			g.lineTo(topLeftPoint.x + (bottomRightPoint.x-topLeftPoint.x)/2,bottomRightPoint.y);
		}
	}
}